package org.pro.Model;

import java.awt.*;
import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArrayList;

public class Tank extends Element {
    public double dir = 90.0;
    public double turretDir;
    public boolean moving = false;
    public long moveSteps = 0L;
    public double speed = 200.0;
    public double hp = 600.0;
    public double hpmax = 200.0;
    public double hp_recovery_per_sec = 0.1;
    public int team = 1;
    public int shotscount=0;//记录坦克子弹数量
    transient public DrawInterface drawInterface;
    public static final long serialVersionUID = 2L;

    public Tank() {
    }

    public Tank(int x, int y, double dir, double hp, double hp_recovery_per_sec, int team) {
        this.x = (double)x;
        this.y = (double)y;
        this.dir = dir;
        this.speed = this.speed;
        this.hp = hp;
        this.hp_recovery_per_sec = hp_recovery_per_sec;
        this.team = team;
        this.width=36;
        this.height=38;
    }

    public void damage(double val) {
        this.hp -= val;
        if (this.hp <= 0.0) {
            System.out.println("坦克销毁");
            this.destroy();
        }

    }

    public void update(double timeFlaps,CopyOnWriteArrayList<Element> elements) {
        if (!this.Destroyed) {
            this.recoverLife();
            if (this.moving) {
                double len = this.speed * timeFlaps;
                this.moveSteps++;
                this.move(this.dir, len,elements);
            }
        }
    }

    public void recoverLife() {
        this.hp += this.hp_recovery_per_sec;
        if (this.hp > this.hpmax) {
            this.hp = this.hpmax;
        }

    }

    public void draw(Graphics2D g, Object panel, CopyOnWriteArrayList<Gross> grossList) {
        if (!isInGross(grossList)) { // 仅在非草丛区域绘制
            this.drawInterface = (DrawInterface)panel;
            this.drawInterface.draw(this, g);
        }
    }

    public interface DrawInterface {
        void draw(Tank var1, Graphics2D var2);
    }
    //这个就不好更新了勒
    // 替换checkgetx和checkgety方法
    public boolean canMove(CopyOnWriteArrayList<Element> elements, double dx, double dy) {
        Rectangle futureRect = new Rectangle(
                (int)(this.x + dx - this.width/2),
                (int)(this.y + dy - this.height/2),
                this.width,
                this.height
        );

        for (Element e : elements) {
            if (e instanceof River) {
                if (futureRect.intersects(e.getRect())) {
                    return false;
                }
            }
        }
        return true;
    }

    // 修改move方法
    public void move(double dir, double len, CopyOnWriteArrayList<Element> elements) {
        double dx = len * Math.cos(Math.toRadians(dir - 90));
        double dy = len * Math.sin(Math.toRadians(dir - 90));
        if (canMove(elements, dx, dy)) {
            this.x += dx;
            this.y += dy;
        }
    }
    //碰撞检测方法
    public boolean isInGross(CopyOnWriteArrayList<Gross> grossList) {
        Rectangle tankRect = this.getRect();
        for (Gross gross : grossList) {
            if (tankRect.intersects(gross.getRect())) {
                return true;
            }
        }
        return false;
    }
}

